<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <!-- span 表示一行占多少份 -->
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="addA('')"
                    >添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="data" :props="defaultProps" default-expand-all>
          <!-- eslint-disable-next-line vue/no-template-shadow -->
          <template slot-scope="{ node, data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <!-- span 表示一行占多少份 -->
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addA(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="setA(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="delA(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <!-- 弹窗 -->
    <el-dialog
      :title="flag ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="hDialogClose"
    >
      <dept-dialog
        ref="refDeptdialog"
        :origin-list="originList"
        :pid="pid"
        :flag="flag"
        @boolean="showDlog"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { arrayToTree } from '@/utils/index'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      data: [],
      showDialog: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      pid: '',
      flag: false,
      // 收集子组件需要校验的数据
      originList: []
    }
  },
  created() {
    this.departments()
  },
  methods: {
    // 页面加载渲染数据
    async departments() {
      const res = await getDepartments()
      // 把第一项删除掉
      res.data.depts.shift()
      // 调用工具函数
      this.data = arrayToTree(res.data.depts)
      // 从数据里面得到想要校验的数据
      this.originList = res.data.depts.map((item) => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name,
          code: item.code
        }
      })
    },
    // 点击添加门显示添加弹窗
    addA(id) {
      this.showDialog = true
      this.pid = id
      // 转换为添加状态
      this.flag = false
    },
    showDlog() {
      // 点击确认添加，关闭弹窗
      this.showDialog = false
      // 重新渲染页面数据
      this.departments()
    },
    // 编辑添加部门显示添加弹窗
    setA(id) {
      this.showDialog = true
      this.pid = id
      // 转换为编辑状态
      this.flag = true
      // 异步获取子组件
      this.$nextTick(() => {
        // console.log(this.$refs.refDeptdialog)
        // 找到子组件实例对象，并调用方法 ， 获取部门详情
        this.$refs.refDeptdialog.loadDetail()
      })
    },
    // 删除部门
    delA(id) {
      this.$confirm('你确定要删除吗？', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确认',
        type: 'warning'
      })
        .then(async() => {
          const res = await delDepartment(id)
          this.$message.success(res.message)
          this.departments()
        })
        .catch(() => {})
    },
    // 关闭弹窗重置数据清空校验结果
    hDialogClose() {
      this.$refs.refDeptdialog.restForm()
    }
  }
}
</script>

<style scoped>
.el-dropdown {
  cursor: pointer;
}
</style>
